package com.boge.study.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中，a.txt文件中的单词用回车符分隔，b.txt文件中用回车或空格进行分隔。
 * @author Administrator
 *
 */
public class MergeFile {
	
	public static void main(String[] args) {
		new MergeFile().merge();
	}
	
	public void merge() {
		Reader reader1 = null;
		Reader reader2 = null;
		Writer writer = null;
		try {
			//获取项目根路径
			File file = new File("");
			String courseFile = file.getCanonicalPath();
			
			String filePath1 = courseFile + "/testFile/a";
			String filePath2 = courseFile + "/testFile/b";
			String filePath3 = courseFile + "/testFile/c";
			List<Character> dataList1 = new ArrayList<Character>();
			List<Character> dataList2 = new ArrayList<Character>();
			List<Character> dataList3 = new ArrayList<Character>();
			
			reader1 = new InputStreamReader(new FileInputStream(filePath1));
			reader2 = new InputStreamReader(new FileInputStream(filePath2));
			
			//一次读取多个字符
//			char[] tempchars = new char[30];
//			int charread = 0;
//			while ((charread = reader1.read(tempchars)) != -1) {
//				// 对于windows下，\r\n这两个字符在一起时，表示一个换行。
//                // 但如果这两个字符分开显示时，会换两次行。
//                // 因此，屏蔽掉\r，或者屏蔽\n。否则，将会多出很多空行。 ???
//				if (charread == tempchars.length ) {
//					System.out.print(tempchars);
//				} else {
//					for (int i = 0; i < charread; i++) {
//						System.out.print(tempchars[i]);
//					}
//				}
//			}
			
			System.out.println("----------------");
			
			//单个字符读取
			int tempchar;
			while ((tempchar = reader1.read()) != -1) {
				if ((char)tempchar != '\n' && (char)tempchar != '\r') {
					dataList1.add((char)tempchar);
				}
				
			}
			
			while ((tempchar = reader2.read()) != -1) {
				if ((char)tempchar != ' ' && (char)tempchar != '\n' && (char)tempchar != '\r') {
					dataList2.add((char)tempchar);
				}
			}
			
			//写入c文件 不覆盖
			writer = new FileWriter(filePath3, true);
			writer.append("\r\n");
			
			int a = dataList1.size();
			System.out.println("dataList1:" + dataList1.size());
			int b = dataList2.size();
			System.out.println("dataList2:" + dataList2.size());
			if (a >= b) {
				for (int i = 0; i < b; i++) {
					dataList1.add(2 * i + 1, dataList2.get(i));
				}
				dataList3 = dataList1;
			} else {
				for (int i = 0; i < a; i++) {
					dataList2.add(2 * i, dataList1.get(i));
					dataList3 = dataList2;
				}
			}
			
			for (Character character : dataList3) {
				writer.append(character);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (reader1 != null) {
					reader1.close();
				}
				if (reader2 != null) {
					reader2.close();
				}
				if (writer != null) {
					writer.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
}
